JavaScript syntax
part 19/30 Β· 107.0 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Examples:
const x = 11 & 6;
console.log(x); // 2
JavaScript supports the following unary bitwise operator:
Bitwise Assignment
JavaScript supports the following binary assignment operators:
| &= | and |
|---|---|
| /= | or |
| ^= | xor |
| <<= | shift left (zero fill at right) |
| >>= | shift right (sign-propagating); copies⦠|
| >>>= | shift right (zero fill at left). For po⦠|
Examples:
let x=7;
console.log(x); // 7
x<<=3;
console.log(x); // 7->14->28->56
String
| = | assignment |
|---|---|
| + | concatenation |
| += | concatenate and assign |
Examples:
let str = "ab" + "cd"; // "abcd"
str += "e"; // "abcde"
const str2 = "2" + 2; // "22", not "4" or 4.
??
JavaScript's nearest operator is ??, the "nullish coalescing operator",
which was added to the standard in ECMAScript's 11th edition.cite-ref-18[18] In
earlier versions, it could be used via a Babel plugin, and in
TypeScript. It evaluates its left-hand operand and, if the result value
is not "nullish" (null or undefined), takes that value as its result;
otherwise, it evaluates the right-hand operand and takes the resulting
value as its result.
In the following example, a will be assigned the value of b if the value
of b is not null or undefined, otherwise it will be assigned 3.
const a = b ?? 3;
Before the nullish coalescing operator, programmers would use the
logical OR operator (||). But where ?? looks specifically for null or
"", 0, NaN, and of course, false.
In the following example, a will be assigned the value of b if the value
of b is truthy, otherwise it will be assigned 3.
const a = b || 3;
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ